package com.webcode.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.webcode.pojo.User;
import com.webcode.service.UserService;
import com.webcode.service.impl.UserServiceImpl;

/**
 * @ClassName LoginServlet.java
 * @Description TODO
 * @Author wushaopei
 * @Date 2020年9月19日
 * @Version 1.0
 */
public class LoginServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	private UserService userService = new UserServiceImpl();

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//获取请求的参数
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		// 调用 userService.login()检查登录
		User loginUser = userService.login(new User(null,username,password,null));
		if( loginUser == null ){
			// 保存需要回显的信息
			request.setAttribute("msg", "登录失败，用户名或密码错误！");
			request.setAttribute("username", username);
			// 登录失败 /pages/user/login.html
			request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
		}else{
			request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request, response);
		}						
	}
}
